package com.cyx.gametool.mgr;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.db.ds.simple.SimpleDataSource;
import com.cyx.gametool.base.CallBack;
import com.cyx.gametool.base.EmptyCallBack;
import com.cyx.gametool.cons.ExcelGroup;
import com.cyx.gametool.cons.Result;
import com.cyx.gametool.excel.ExcelTableData;
import com.cyx.gametool.view.*;
import lombok.Getter;
import lombok.Setter;

import javax.sql.DataSource;
import java.io.File;
import java.sql.Connection;
import java.util.List;

/**
 * 导表工具
 */
public class ExcelToSomeMgr {

    // 单例模式
    @Getter
    private static ExcelToSomeMgr instance = new ExcelToSomeMgr();

    /**
     * Excel视图设置信息
     */
    @Getter
    @Setter
    private ExcelConfigField excelConfigField;

    /**
     * excel转mysql视图信息
     */
    @Getter
    @Setter
    private ExcelToMysqlField excelToMysqlField;

    /**
     * excel转java类文件视图信息
     */
    @Getter
    @Setter
    private ExcelToJavaField excelToJavaField;

    /**
     * excel转json文件视图信息
     */
    @Getter
    @Setter
    private ExcelToJsonField excelToJsonField;

    private ExcelToSomeMgr() {
    }

    public static void main(String[] args) {
        List<ExcelGroup> groupList = Convert.toList(ExcelGroup.class, "[MYSQL,JAVA,JSON]");
        System.out.println(groupList);
    }

    // 检测数据库连接

    /**
     * 测试数据库连接
     *
     * @param index 数据库配置索引
     */
    public void testMysqlConnect(int index) {
        testMysqlConnect(index, new EmptyCallBack());
    }


    /**
     * 测试数据库连接
     *
     * @param index 数据库配置索引
     */
    public void testMysqlConnect(int index, CallBack callBack) {
        if (excelToMysqlField == null) {
            callBack.onCallBack(Result.error());
            return;
        }
        List<MysqlField> allConfig = excelToMysqlField.getAllConfig();
        if (CollUtil.size(allConfig) <= index) {
            callBack.onCallBack(Result.error());
            return;
        }
        MysqlField mysqlField = allConfig.get(index);

        String userName = mysqlField.getUsername();
        String password = mysqlField.getPassword();

        String dbUrl = "jdbc:mysql://" + mysqlField.getHost() + ":" + mysqlField.getPort() + "/" + mysqlField.getDatabase();
        ThreadUtil.execAsync(() -> {
            try {
                DataSource ds = new SimpleDataSource(dbUrl, userName, password);
                Connection connection = ds.getConnection();
                if (connection != null) {
                    callBack.onCallBack(Result.success());
                } else {
                    callBack.onCallBack(Result.fail());
                }
                ds.getConnection().close();
            } catch (Exception e) {
                callBack.onCallBack(Result.error());
            }
        });
    }

    /**
     * 执行转换事件
     *
     * @param excelTableData
     * @param chooseExecutors
     * @param callBack
     */
    private void excelToThing(ExcelTableData excelTableData, List<ExcelGroup> chooseExecutors, CallBack callBack) {
        System.out.println("excelToThing");
    }

    /**
     * 判断文件是否为Excel文件
     *
     * @param file 文件对象
     * @return 如果是Excel文件返回true，否则返回false
     */
    private boolean isExcelFile(File file) {
        String fileName = file.getName();
        return fileName.endsWith(".xlsx") || fileName.endsWith(".xls");
    }
}
